<template>
  <div class="user-box" v-loading="loading">
    <div>
      <topTitle title="用户信息" style="padding-left: 30px" />
      <!-- 上方用户信息 -->
      <div class="user-top">
        <div class="user-area">
          <div class="user_img">
            <img :src="userVo.userImage" alt="" />
          </div>
          <div class="user-name">
            <div>{{ userVo.nickName || "-" }}</div>
            <div>
              <span>{{ userVo.userId }}</span>
              <span
                style="margin-left: 5px"
                class="table-btn"
                @click="copyUserId(userVo.userId)"
                >复制</span
              >
            </div>
            <div>{{ userVo.roleNames }}</div>
          </div>
          <el-button type="primary" size="small" class="add-sub" @click="addSub"
            >添加备注</el-button
          >
        </div>
        <div class="actions">
          <div class="user_btn">
            <!-- <el-button
              type="danger"
              class="btn"
              style="margin-left: 20px"
              @click="addBlackName(1)"
              >添加黑名单</el-button
            > -->
            <div v-if="userVo.userStatus === '0'">
              <el-button
                style="width: 100%; margin-right: 10px"
                type="info"
                @click="updateStatus('1')"
                >设为正常用户</el-button
              >
            </div>
            <AddBlackName
              :sourceType="1"
              @updateList="updateList"
              :userId="userVo.userId"
            />
          </div>
        </div>
      </div>
      <!-- 备注列表 -->
      <div class="sub-wrap" v-if="userRemarks && userRemarks.length">
        <div class="sub-title">
          <span>备注</span>
        </div>
        <template>
          <div class="sub-list" :style="{ height: allFlag ? 'auto' : '200px' }">
            <div
              class="sub-row"
              v-for="item in userRemarks"
              :key="item.remarkId"
            >
              <div class="top">
                <div class="name">{{ item.nickName }}</div>
                <div class="time">{{ item.updateTime }}</div>
                <i
                  class="el-icon-edit"
                  style="color: #409eff; margin-right: 10px"
                  @click="addSub(item)"
                ></i>
                <i
                  class="el-icon-delete"
                  style="color: #f56c6c"
                  @click="delSub(item)"
                ></i>
              </div>
              <div class="bottom">{{ item.remark }}</div>
              <div style="color: #909399; font-size: 12px" v-if="item.source">
                来源：{{ item.source }}
                <span class="table-btn" @click="goDetail(item)">{{
                  item.number
                }}</span>
              </div>
            </div>
          </div>
          <div class="sub-botton" @click="showAll">
            <i class="el-icon-caret-bottom"></i>
          </div>
        </template>
      </div>
      <div class="user-tabs">
            <el-tabs :value="userTabValue">
                <el-tab-pane label="基本信息" name="first" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="绑定信息" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="安装包信息" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="邀请人信息" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="提现记录" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="游戏记录" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="收藏记录" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="金币记录" :lazy="true">
                </el-tab-pane>
                <el-tab-pane label="碎片记录" :lazy="true">
                </el-tab-pane>
            </el-tabs>
        </div>
      <el-dialog :title="placeholderTxt" :visible.sync="banned" width="400px">
        <div style="padding: 10px 0 20px">
          <el-input v-model="remark" type="textarea"></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="banned = false">取 消</el-button>
          <el-button type="primary" @click="getUpdateStatus">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 添加备注信息 -->
      <el-dialog :title="subTitle" :visible.sync="subFlag" width="400px">
        <div style="padding: 10px 0 20px">
          <el-input
            v-model="subContent"
            maxlength="200"
            show-word-limit
            type="textarea"
          ></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="subFlag = false">取 消</el-button>
          <el-button type="primary" @click="subConfirm">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { formatDate } from "@/utils/utils";
export default {
  components: {},
  data() {
    return {
      banned: false,
      remark: "",
      status: 1,
      userId: "",
      userTabValue: "first",
      showUser: false,
      loading: false,
      detail: {},
      placeholderTxt: "",
      subFlag: false, // 添加备注信息
      subContent: "",
      subTitle: "添加备注",
      subRemarkId: null,
      properties: [],
      userRemarks: [],
      userVo: {},
      allFlag: false,
      maxCount: 0,
      counts: {},
    };
  },
  created() {
    if (this.$route.query.id) {
      this.userId = this.$route.query.id;
      this.getUserView();
      this.getAdviseUserTapCount();
    }
  },

  methods: {
    // 复制用户
    copyUserId(item) {
      const aux = document.createElement("textarea");
      aux.innerHTML = item;
      document.body.appendChild(aux);
      aux.select();
      document.execCommand("copy");
      document.body.removeChild(aux);
      this.$message.success("复制成功");
    },
    // 获取数目
    async getAdviseUserTapCount() {
      let res = await this.$http.get(
        `/user/adviseUserTapCount?userId=${this.userId}`,
        {}
      );
      if (res.code === "0") {
        this.nums = res.data;
      } else {
        this.$message({
          type: "error",
          message: "系统异常，请稍后再试",
        });
      }
    },
    updateList() {
      this.$refs.balckItem.getList();
      this.loading = false;
      this.getUserView();
    },
    checkboxChange(arr) {
      this.roleArr = arr;
    },
    // 添加备注
    addSub(content) {
      this.subFlag = true;
      this.subContent = "";
      if (content && content.remarkId) {
        this.subRemarkId = content.remarkId;
        this.subTitle = "编辑备注";
        this.subContent = content.remark;
      }
    },
    goDetail(item) {
      if (!item.jumpType) {
        this.$message({
          type: "error",
          message: "跳转失败",
        });
      } else if (item.jumpType === 1) {
        const { href } = this.$router.resolve({
          path: "/seller/accTradingDetail",
          query: { dataId: item.businessId, status, type: 0 },
        });
        window.open(href, "_blank");
      } else if (item.jumpType === 2) {
        const { href } = this.$router.resolve({
          path: "/seller/tradOrderDetail",
          query: { orderId: item.businessId },
        });
        window.open(href, "_blank");
      }
    },
    // 展开全部
    showAll() {
      this.allFlag = !this.allFlag;
    },
    // 确定添加或编辑备注
    subConfirm() {
      let params = {
        beRemarkUserId: this.userId,
        remark: this.subContent,
      };
      let url = `/user/addRemark`;
      if (this.subRemarkId) {
        url = `/user/editRemark`;
        params.remarkId = this.subRemarkId || 0;
      }
      this.$http.post(url, params).then((res) => {
        if (res.code === "0") {
          this.subFlag = false;
          this.getUserView();
          this.$message({
            type: "success",
            message: this.subRemarkId ? "编辑成功" : "添加成功",
          });
          this.subRemarkId = null;
        } else {
          this.$message({
            type: "error",
            message: res.desc,
          });
        }
      });
    },
    // 删除备注
    delSub(item) {
      this.$http
        .post(`/user/delRemark`, {
          remarkId: item.remarkId,
        })
        .then((res) => {
          if (res.code === "0") {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getUserView();
          } else {
            this.$message({
              type: "error",
              message: res.desc,
            });
          }
        });
    },

    // 获取用户详情
    async getUserView() {
      if (this.loading) return;
      this.loading = true;
      let res = await this.$http.get("/user/getUserView", {
        params: {
          userId: this.userId,
        },
      });
      this.loading = false;
      if (res.code === "0") {
        this.properties = res.data.properties;
        this.userVo = res.data.userVo;
        this.userRemarks = res.data.userRemarks;
        this.maxCount = res.data.maxCount;
        this.counts = res.data.counts;
        this.roleArr =
          (res.data.userVo && res.data.userVo.roleNameDescList) || [];
      } else {
        this.$message({
          type: "error",
          message: res.desc,
        });
      }
    },

    // 设为视频合作方
    async updateIdentifys(status) {
      if (this.loading) return;
      this.loading = true;
      let res = await this.$http.get("/user/updateIdentifys", {
        params: {
          userId: this.detail.userId,
          status,
        },
      });
      if (res.code === "0") {
        this.detail.identify = status;
        this.$message({
          type: "success",
          message: "设置成功",
        });
      } else {
        this.$message({
          type: "error",
          message: res.desc,
        });
      }
      this.loading = false;
    },

    // 封禁（解封）用户
    updateStatus(status) {
      this.loading = false;
      // 设置正常用户不需要弹框
      if (status === "1") {
        this.status = status;
        this.placeholderTxt = "";
        this.getUpdateStatus();
        return;
      }
      this.banned = true;
      this.status = status;
      let obj = {
        0: "请输入不可提现原因",
        1: "请输入设为正常用户原因",
        "-1": "请输入封禁原因",
        "-2": "请输入冻结钱包原因",
      };
      this.placeholderTxt = obj[status];
    },

    async getUpdateStatus() {
      if (this.loading) return;
      this.loading = true;
      let res = await this.$http.get("/user/updateStatus", {
        params: {
          userId: this.userVo.userId,
          status: this.status,
          message: this.remark,
        },
      });
      this.loading = false;
      if (res.code === "0") {
        // 用户状态 -1封禁  0不可提现   1正常用户 -2资金冻结
        this.userVo.userStatus = this.status;
        this.banned = false;
        this.getUserView();
        this.$message({
          type: "success",
          message: "设置成功",
        });
      } else {
        this.$message({
          type: "error",
          message: res.desc,
        });
      }
      this.remark = "";
    }
  },

  filters: {
    timeInit(val) {
      if (val) {
        return formatDate(val, "YYYY-MM-DD HH:MM:SS");
      } else {
        return "-";
      }
    },
  },

  beforeRouteLeave(to, from, next) {
    delete from.meta.parent;
    console.log(from);
    next();
  },
};
</script>

<style lang="scss" scoped>
.role-wrap {
  padding: 10px;
  display: flex;
}
.user-box {
  margin: 0px auto 0;
  padding-bottom: 40px;
  border-radius: 4px;
  border: solid 1px #dfdfdf;
  border-top: none;
  background-color: #ffffff;

  .sub-wrap {
    border: solid 1px #ededed;
    border-radius: 5px;
    margin: 0px 30px;
    margin-bottom: 20px;
    overflow: hidden;
    .sub-title {
      padding: 10px;
      font-size: 16px;
      font-weight: 500;
      color: #363636;
      position: relative;
    }
    .sub-botton {
      padding: 5px 0px 10px;
      text-align: center;
      border-top: solid 1px #ededed;
      position: relative;
      background-color: #ffffff;
    }
    .sub-list {
      border-top: solid 1px #ededed;
      padding: 10px;
      height: 200px;
      .sub-row {
        margin-bottom: 10px;
        .top {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-bottom: 5px;
          .name {
            font-size: 14px;
            font-weight: 500;
            color: #363636;
            margin-right: 10px;
          }
          .time {
            font-size: 12px;
            font-weight: 500;
            color: #9ba0b1;
            margin-right: 10px;
          }
          i {
            cursor: pointer;
          }
        }
        .bottom {
          font-size: 14px;
          color: #363636;
          word-wrap: break-word;
        }
      }
    }
  }
  .user-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 30px;
    margin-bottom: 20px;
    .user-area {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .user_img {
        width: 50px;
        height: 50px;
        overflow: hidden;
        border-radius: 50px;
        background-color: #ccc;
        margin-right: 20px;
        img {
          width: 50px;
          height: 50px;
        }
      }
      .user-name {
        margin-right: 20px;
        > div {
          font-size: 12px;
          font-weight: 600;
          color: #9ba0b1;
          margin-bottom: 1px;
        }
      }
    }
    .user_btn {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      > div {
        margin-right: 20px;
      }
    }
  }

  .user-msg {
    display: flex;
    align-items: center;
    padding: 20px;
    border-radius: 4px;
    border: solid 1px #ededed;
    background-color: #fdfdfd;
    margin: 0 30px 30px;

    .user-info-box {
      flex: 1;
      margin: 0 20px;
      .user-info-item {
        display: flex;
        margin-bottom: 10px;
        .user_box {
          // padding: 5px;
          // min-width: 100px;
          flex: 1;
        }
        &:last-child {
          margin: 0;
        }
        .rating {
          display: flex;
          align-items: center;
          i {
            cursor: pointer;
            color: #c6c6c6;
            margin-left: 5px;
          }
        }
        .user_msg {
          font-size: 14px;
          font-weight: 500;
          color: #393939;
          display: flex;
          justify-content: flex-start;
          .tip_waring {
            cursor: pointer;
          }
        }
        .user_status {
          font-size: 14px;
          font-weight: 600;
          color: #00ba45;
        }
        .user_status_bad {
          font-size: 14px;
          font-weight: 600;
          color: red;
        }
        .user_title {
          font-size: 12px;
          font-weight: 600;
          color: #9ba0b1;
          margin-bottom: 6px;
        }
      }
    }
  }
  .table-btn {
    cursor: pointer;
    color: #409eff;
    font-size: 13px;
    font-weight: 500;
    &:hover {
      text-decoration: underline;
    }
  }
  .user-tabs {
    margin: 0px 30px;
  }
}
</style>
